import QtQuick 1.1
import com.nokia.meego 1.0
import "core"
import "harmattan"

PageStackWindow {
    id: appWindow

    initialPage: game

    states: [
        State {
            name: "fullsize-visible"
            when: platformWindow.viewMode == WindowState.Fullsize && platformWindow.visible
            StateChangeScript {
                script: {
                    if (pageStack.currentPage == standbyPage) { pageStack.pop() }
                    game.resume();
                    console.log("Visibility: Fullsize and visible!")
                }
            }
        },
        State {
            name: "thumbnail-or-invisible"
            when: platformWindow.viewMode == WindowState.Thumbnail || !platformWindow.visible
            StateChangeScript {
                script: {
                    if (pageStack.currentPage == game) { pageStack.push(standbyPage) }
                    game.pause();
                    console.log("Visibility: Not fullsize (Thumbnail and/or invisible)")
                }
            }
        }
    ]

    QueryDialog {
        id: endDialog
        titleText : 'You ' + (won ? 'won' : 'lost') + '!'
        message: 'You ' + (won ? 'won' : 'lost') + ' the Game!'+ (won ? '\nYour time: ' + (game.getTime()) : '') + '\nPlay another?';
        acceptButtonText: 'Play'
        rejectButtonText: 'Exit'
        onAccepted:  {
            game.newGame();
            endDialog.close();
        }
        onRejected: Qt.quit();

        property bool won: false
    }

    Game {
        id: game
        tools: commonTools
        columns: settingsCont.value('table/columns', 10)
        rows: settingsCont.value('table/rows', 10)
        mines: settingsCont.value('table/mines', 10)

        onEnd: {
            endDialog.won = won;
            endDialog.open();
        }
    }

    StandByPage {
        id: standbyPage
    }

    Settings {
        id: settings
        onSet: {
            game.columns = numOfColumns;
            game.rows = numOfRows;
            game.mines = numOfMines;
            settingsCont.setValue('table/columns', numOfColumns);
            settingsCont.setValue('table/rows', numOfRows);
            settingsCont.setValue('table/mines', numOfMines);
            game.initialize();
            game.newGame();
            appWindow.pageStack.pop();
        }
        onClose: {
            game.resume();
            appWindow.pageStack.pop()
        }
    }

    About {
        id: about
        onClose: {
            game.resume();
            appWindow.pageStack.pop()
        }
    }

    ToolBarLayout {
        id: commonTools
        visible: true
        ToolButton {
            text: '' + game.flags + ' / ' + game.mines
            iconSource: 'core/images/flag.svg'
        }
        ToolButton {
            id: timeCount
            text: '00:00'
        }
        ToolIcon {
            platformIconId: "toolbar-view-menu";
            anchors.right: parent===undefined ? undefined : parent.right
            onClicked: (myMenu.status == DialogStatus.Closed) ? myMenu.open() : myMenu.close()
        }
    }

    Menu {
        id: myMenu
        visualParent: pageStack
        MenuLayout {
            MenuItem {
                text: "New Game";
                onClicked : {
                    game.newGame();
                }
            }
            MenuItem {
                text: "Settings";
                onClicked: {
                    game.pause();
                    appWindow.pageStack.push(settings);
                }
            }
            MenuItem {
                text: "About";
                onClicked: {
                    game.pause();
                    appWindow.pageStack.push(about);
                }
            }
        }
    }    
}
